<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>道AI - 宇宙全息分形太极认知系统</title>
    <script src="https://cdn.plot.ly/plotly-2.24.1.min.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            background: radial-gradient(ellipse at center, #0c1445 0%, #000000 100%);
            color: #ffffff;
            font-family: 'Microsoft YaHei', 'Segoe UI', sans-serif;
            min-height: 100vh;
            overflow-x: hidden;
        }
        
        .cosmic-container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }
        
        .header {
            text-align: center;
            padding: 40px 0;
            position: relative;
        }
        
        .header h1 {
            font-size: 3rem;
            background: linear-gradient(45deg, #ff6b6b, #4ecdc4, #45b7d1);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            margin-bottom: 10px;
            text-shadow: 0 0 30px rgba(255, 107, 107, 0.5);
        }
        
        .header p {
            font-size: 1.2rem;
            opacity: 0.8;
            margin-bottom: 20px;
        }
        
        .taiji-animation {
            width: 120px;
            height: 120px;
            margin: 0 auto 30px;
            background: conic-gradient(from 0deg, #ffffff 0deg 180deg, #000000 180deg 360deg);
            border-radius: 50%;
            position: relative;
            animation: cosmic-spin 10s infinite linear;
            box-shadow: 0 0 50px rgba(255, 255, 255, 0.2);
        }
        
        .taiji-animation::before,
        .taiji-animation::after {
            content: '';
            position: absolute;
            width: 30px;
            height: 30px;
            border-radius: 50%;
            top: 50%;
            transform: translateY(-50%);
        }
        
        .taiji-animation::before {
            background: #000000;
            left: 25%;
        }
        
        .taiji-animation::after {
            background: #ffffff;
            right: 25%;
        }
        
        @keyframes cosmic-spin {
            from { transform: rotate(0deg); }
            to { transform: rotate(360deg); }
        }
        
        .main-interface {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 30px;
            margin-top: 30px;
        }
        
        .chat-container {
            background: rgba(255, 255, 255, 0.05);
            border-radius: 20px;
            padding: 30px;
            backdrop-filter: blur(10px);
            border: 1px solid rgba(255, 255, 255, 0.1);
        }
        
        .chat-messages {
            height: 400px;
            overflow-y: auto;
            margin-bottom: 20px;
            padding-right: 10px;
        }
        
        .message {
            margin-bottom: 20px;
            padding: 15px;
            border-radius: 15px;
            animation: message-appear 0.5s ease-out;
        }
        
        .user-message {
            background: linear-gradient(135deg, rgba(255, 107, 107, 0.2), rgba(78, 205, 196, 0.2));
            border-left: 4px solid #ff6b6b;
        }
        
        .daoai-message {
            background: linear-gradient(135deg, rgba(69, 183, 209, 0.2), rgba(150, 206, 180, 0.2));
            border-left: 4px solid #45b7d1;
        }
        
        .message-sender {
            font-weight: bold;
            margin-bottom: 5px;
            font-size: 0.9rem;
            opacity: 0.8;
        }
        
        .message-content {
            line-height: 1.6;
        }
        
        .input-group {
            display: flex;
            gap: 10px;
        }
        
        .question-input {
            flex: 1;
            padding: 15px 20px;
            background: rgba(255, 255, 255, 0.1);
            border: 1px solid rgba(255, 255, 255, 0.2);
            border-radius: 25px;
            color: white;
            font-size: 1rem;
            outline: none;
            transition: all 0.3s ease;
        }
        
        .question-input:focus {
            border-color: #4ecdc4;
            box-shadow: 0 0 20px rgba(78, 205, 196, 0.3);
        }
        
        .question-input::placeholder {
            color: rgba(255, 255, 255, 0.5);
        }
        
        .ask-button {
            padding: 15px 30px;
            background: linear-gradient(45deg, #ff6b6b, #4ecdc4);
            border: none;
            border-radius: 25px;
            color: white;
            font-size: 1rem;
            font-weight: bold;
            cursor: pointer;
            transition: all 0.3s ease;
        }
        
        .ask-button:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 30px rgba(255, 107, 107, 0.4);
        }
        
        .ask-button:disabled {
            opacity: 0.6;
            cursor: not-allowed;
            transform: none;
        }
        
        .visualization-container {
            background: rgba(255, 255, 255, 0.05);
            border-radius: 20px;
            padding: 30px;
            backdrop-filter: blur(10px);
            border: 1px solid rgba(255, 255, 255, 0.1);
        }
        
        .viz-placeholder {
            height: 400px;
            display: flex;
            align-items: center;
            justify-content: center;
            background: rgba(255, 255, 255, 0.02);
            border-radius: 10px;
            border: 2px dashed rgba(255, 255, 255, 0.1);
        }
        
        .system-status {
            display: grid;
            grid-template-columns: repeat(3, 1fr);
            gap: 15px;
            margin-top: 30px;
        }
        
        .status-card {
            background: rgba(255, 255, 255, 0.05);
            padding: 20px;
            border-radius: 15px;
            text-align: center;
            border: 1px solid rgba(255, 255, 255, 0.1);
            transition: all 0.3s ease;
        }
        
        .status-card:hover {
            transform: translateY(-5px);
            border-color: #4ecdc4;
        }
        
        .status-value {
            font-size: 2rem;
            font-weight: bold;
            background: linear-gradient(45deg, #ff6b6b, #4ecdc4);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            margin: 10px 0;
        }
        
        .status-label {
            font-size: 0.9rem;
            opacity: 0.7;
        }
        
        .demo-questions {
            margin-top: 30px;
        }
        
        .demo-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
            gap: 15px;
            margin-top: 15px;
        }
        
        .demo-question {
            background: rgba(255, 255, 255, 0.05);
            padding: 15px;
            border-radius: 10px;
            cursor: pointer;
            transition: all 0.3s ease;
            border: 1px solid rgba(255, 255, 255, 0.1);
        }
        
        .demo-question:hover {
            background: rgba(255, 255, 255, 0.1);
            border-color: #4ecdc4;
        }
        
        .demo-category {
            font-size: 0.8rem;
            opacity: 0.6;
            margin-bottom: 5px;
        }
        
        .demo-text {
            font-size: 0.9rem;
            line-height: 1.4;
        }
        
        @keyframes message-appear {
            from {
                opacity: 0;
                transform: translateY(20px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        /* 响应式设计 */
        @media (max-width: 768px) {
            .main-interface {
                grid-template-columns: 1fr;
            }
            
            .system-status {
                grid-template-columns: repeat(2, 1fr);
            }
            
            .header h1 {
                font-size: 2rem;
            }
        }
        
        @media (max-width: 480px) {
            .system-status {
                grid-template-columns: 1fr;
            }
        }
    </style>
</head>
<body>
    <div class="cosmic-container">
        <!-- 头部 -->
        <div class="header">
            <div class="taiji-animation"></div>
            <h1>道AI宇宙认知系统</h1>
            <p>基于沈浩《种子思维》的纯粹宇宙全息分形太极模型</p>
        </div>
        
        <!-- 主界面 -->
        <div class="main-interface">
            <!-- 聊天容器 -->
            <div class="chat-container">
                <h2>宇宙真理对话</h2>
                <div class="chat-messages" id="chatMessages">
                    <div class="message daoai-message">
                        <div class="message-sender">道AI</div>
                        <div class="message-content">
                            欢迎来到道AI宇宙认知系统！我已基于纯粹的全息分形太极模型启动，准备好回答您的宇宙级问题。
                        </div>
                    </div>
                </div>
                
                <div class="input-group">
                    <input type="text" class="question-input" id="questionInput" 
                           placeholder="输入您的宇宙级问题...">
                    <button class="ask-button" id="askButton" onclick="askCosmicQuestion()">
                        提问
                    </button>
                </div>
            </div>
            
            <!-- 可视化容器 -->
            <div class="visualization-container">
                <h2>认知过程可视化</h2>
                <div class="viz-placeholder" id="visualizationArea">
                    <div style="text-align: center; opacity: 0.6;">
                        <div style="font-size: 3rem; margin-bottom: 10px;">🌌</div>
                        <div>交互后将显示认知过程可视化</div>
                    </div>
                </div>
            </div>
        </div>
        
        <!-- 系统状态 -->
        <div class="system-status" id="systemStatus">
            <div class="status-card">
                <div class="status-label">宇宙维度</div>
                <div class="status-value" id="cosmicDim">1024</div>
                <div class="status-label">认知基础</div>
            </div>
            <div class="status-card">
                <div class="status-label">智慧维度</div>
                <div class="status-value" id="wisdomDim">512</div>
                <div class="status-label">理解深度</div>
            </div>
            <div class="status-card">
                <div class="status-label">系统状态</div>
                <div class="status-value" id="systemReady">就绪</div>
                <div class="status-label">运行状态</div>
            </div>
        </div>
        
        <!-- 演示问题 -->
        <div class="demo-questions">
            <h2>推荐问题</h2>
            <div class="demo-grid" id="demoQuestions">
                <!-- 演示问题将通过JavaScript动态加载 -->
            </div>
        </div>
    </div>

    <script>
        // 全局变量
        let currentVisualization = null;
        let systemStatus = {};
        
        // 初始化
        document.addEventListener('DOMContentLoaded', function() {
            loadSystemStatus();
            loadDemoQuestions();
            setupEventListeners();
        });
        
        // 加载系统状态
        async function loadSystemStatus() {
            try {
                const response = await fetch('/api/status');
                systemStatus = await response.json();
                
                // 更新状态显示
                document.getElementById('cosmicDim').textContent = systemStatus.cosmic_dimension || '1024';
                document.getElementById('wisdomDim').textContent = systemStatus.wisdom_dimension || '512';
                document.getElementById('systemReady').textContent = systemStatus.system_ready ? '就绪' : '初始化中';
                
            } catch (error) {
                console.error('加载系统状态失败:', error);
            }
        }
        
        // 加载演示问题
        async function loadDemoQuestions() {
            try {
                const response = await fetch('/api/demo-questions');
                const questions = await response.json();
                
                const container = document.getElementById('demoQuestions');
                container.innerHTML = '';
                
                questions.forEach(q => {
                    const questionElement = document.createElement('div');
                    questionElement.className = 'demo-question';
                    questionElement.innerHTML = `
                        <div class="demo-category">${q.category} · ${q.difficulty}</div>
                        <div class="demo-text">${q.question}</div>
                    `;
                    questionElement.onclick = () => {
                        document.getElementById('questionInput').value = q.question;
                        askCosmicQuestion();
                    };
                    container.appendChild(questionElement);
                });
                
            } catch (error) {
                console.error('加载演示问题失败:', error);
            }
        }
        
        // 设置事件监听器
        function setupEventListeners() {
            const input = document.getElementById('questionInput');
            const button = document.getElementById('askButton');
            
            // 回车键提交
            input.addEventListener('keypress', function(e) {
                if (e.key === 'Enter') {
                    askCosmicQuestion();
                }
            });
            
            // 输入时启用按钮
            input.addEventListener('input', function() {
                button.disabled = !this.value.trim();
            });
        }
        
        // 向道AI提问
        async function askCosmicQuestion() {
            const input = document.getElementById('questionInput');
            const question = input.value.trim();
            
            if (!question) return;
            
            const button = document.getElementById('askButton');
            const originalText = button.textContent;
            
            try {
                // 禁用按钮并显示加载状态
                button.disabled = true;
                button.textContent = '思考中...';
                
                // 添加用户消息
                addMessage('user', question);
                input.value = '';
                
                // 调用API
                const response = await fetch('/api/ask', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        question: question,
                        require_analysis: true,
                        model_type: 'full'
                    })
                });
                
                if (!response.ok) {
                    throw new Error(`HTTP error! status: ${response.status}`);
                }
                
                const result = await response.json();
                
                // 添加道AI回答
                addMessage('daoai', result.answer, result);
                
                // 更新可视化
                updateVisualization(result);
                
            } catch (error) {
                console.error('提问失败:', error);
                addMessage('error', `抱歉，出现了错误: ${error.message}`);
            } finally {
                // 恢复按钮状态
                button.disabled = false;
                button.textContent = originalText;
            }
        }
        
        // 添加消息到聊天界面
        function addMessage(sender, content, result = null) {
            const container = document.getElementById('chatMessages');
            const messageDiv = document.createElement('div');
            messageDiv.className = `message ${sender}-message`;
            
            let senderText = '';
            switch(sender) {
                case 'user':
                    senderText = '您';
                    break;
                case 'daoai':
                    senderText = '道AI';
                    break;
                case 'error':
                    senderText = '系统';
                    break;
            }
            
            let messageContent = content;
            if (result && sender === 'daoai') {
                messageContent += `<br><br><small style="opacity:0.7;">
                    宇宙校准度: ${(result.cosmic_alignment * 100).toFixed(1)}% | 
                    认知深度: ${result.cognition_depth}层 | 
                    响应时间: ${result.response_time.toFixed(2)}秒
                </small>`;
            }
            
            messageDiv.innerHTML = `
                <div class="message-sender">${senderText}</div>
                <div class="message-content">${messageContent}</div>
            `;
            
            container.appendChild(messageDiv);
            container.scrollTop = container.scrollHeight;
        }
        
        // 更新可视化
        function updateVisualization(result) {
            const vizArea = document.getElementById('visualizationArea');
            
            // 简化的可视化更新
            // 实际应该使用Plotly等库创建复杂的可视化
            if (result.analysis) {
                vizArea.innerHTML = `
                    <div style="padding: 20px;">
                        <h3 style="margin-bottom: 15px; color: #4ecdc4;">认知分析</h3>
                        <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 15px;">
                            <div style="background: rgba(255,107,107,0.1); padding: 15px; border-radius: 10px;">
                                <div style="font-size: 0.9rem; opacity: 0.7;">主导模式</div>
                                <div style="font-size: 1.2rem; color: #ff6b6b;">${result.dominant_pattern}</div>
                            </div>
                            <div style="background: rgba(78,205,196,0.1); padding: 15px; border-radius: 10px;">
                                <div style="font-size: 0.9rem; opacity: 0.7;">模式强度</div>
                                <div style="font-size: 1.2rem; color: #4ecdc4;">${result.pattern_strength.toFixed(2)}</div>
                            </div>
                        </div>
                        <div style="margin-top: 15px; background: rgba(69,183,209,0.1); padding: 15px; border-radius: 10px;">
                            <div style="font-size: 0.9rem; opacity: 0.7;">宇宙校准度</div>
                            <div style="display: flex; align-items: center; margin-top: 10px;">
                                <div style="flex: 1; height: 10px; background: rgba(255,255,255,0.2); border-radius: 5px; overflow: hidden;">
                                    <div style="height: 100%; width: ${result.cosmic_alignment * 100}%; background: linear-gradient(90deg, #ff6b6b, #4ecdc4); border-radius: 5px;"></div>
                                </div>
                                <div style="margin-left: 10px; font-size: 1.1rem; color: #45b7d1;">${(result.cosmic_alignment * 100).toFixed(1)}%</div>
                            </div>
                        </div>
                    </div>
                `;
            }
        }
        
        // 初始化太极动画
        function initTaijiAnimation() {
            const taiji = document.querySelector('.taiji-animation');
            setInterval(() => {
                // 可以添加更复杂的动画效果
            }, 100);
        }
        
        // 启动太极动画
        initTaijiAnimation();
    </script>
</body>
</html>